package com.zzyl.nursing.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Elder;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.dto.FamilyMemberElderDto;
import com.zzyl.nursing.mapper.ElderMapper;
import com.zzyl.nursing.vo.DeviceDataListVo;
import com.zzyl.nursing.vo.DeviceDataVo;
import com.zzyl.nursing.vo.DevicePropertyStatusVo;
import com.zzyl.nursing.vo.FamilyMemberElderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.FamilyMemberElderMapper;
import com.zzyl.nursing.domain.FamilyMemberElder;
import com.zzyl.nursing.service.IFamilyMemberElderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 家人老人关联关系Service业务层处理
 *
 * @author alexis
 * @date 2025-03-14
 */
@Service
public class FamilyMemberElderServiceImpl extends ServiceImpl<FamilyMemberElderMapper, FamilyMemberElder> implements IFamilyMemberElderService {
    @Autowired
    private FamilyMemberElderMapper familyMemberElderMapper;
    @Autowired
    private ElderMapper elderMapper;

    /**
     * 查询家人老人关联关系
     *
     * @param id 家人老人关联关系主键
     * @return 家人老人关联关系
     */
    @Override
    public FamilyMemberElder selectFamilyMemberElderById(Long id) {
        return familyMemberElderMapper.selectById(id);
    }

    /**
     * 查询家人老人关联关系列表
     *
     * @param familyMemberElder 家人老人关联关系
     * @return 家人老人关联关系
     */
    @Override
    public List<FamilyMemberElder> selectFamilyMemberElderList(FamilyMemberElder familyMemberElder) {
        return familyMemberElderMapper.selectFamilyMemberElderList(familyMemberElder);
    }

    /**
     * 新增家人老人关联关系
     *
     * @param familyMemberElder 家人老人关联关系
     * @return 结果
     */
    @Override
    public int insertFamilyMemberElder(FamilyMemberElder familyMemberElder) {
        return familyMemberElderMapper.insert(familyMemberElder);
    }

    /**
     * 修改家人老人关联关系
     *
     * @param familyMemberElder 家人老人关联关系
     * @return 结果
     */
    @Override
    public int updateFamilyMemberElder(FamilyMemberElder familyMemberElder) {
        return familyMemberElderMapper.updateById(familyMemberElder);
    }

    /**
     * 批量删除家人老人关联关系
     *
     * @param ids 需要删除的家人老人关联关系主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberElderByIds(Long[] ids) {
        return familyMemberElderMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除家人老人关联关系信息
     *
     * @param id 家人老人关联关系主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberElderById(Long id) {
        return familyMemberElderMapper.deleteById(id);
    }

    /**
     * 绑定老人
     *
     * @param familyMemberElderDto
     */
    @Override
    public void bindingElder(FamilyMemberElderDto familyMemberElderDto) {
        //检验老人是否存在
        Elder elder = elderMapper.selectOne(new LambdaQueryWrapper<Elder>()
                .eq(Elder::getIdCardNo, familyMemberElderDto.getIdCard())
                .eq(Elder::getName, familyMemberElderDto.getName()));
        if (ObjectUtil.isNull(elder)) {
            throw new BaseException("老人不存在,请核对老人身份证和姓名是否正确");
        }
        //校验家人是否已绑定该老人
        Long familyMemberId = UserThreadLocal.get();
        FamilyMemberElder familyMemberElder = this.getOne(new LambdaQueryWrapper<FamilyMemberElder>()
                .eq(FamilyMemberElder::getFamilyMemberId, familyMemberId)
                .eq(FamilyMemberElder::getElderId, elder.getId()));
        if (ObjUtil.isNotNull(familyMemberElder)) {
            throw new BaseException("您已绑定该家人,请勿重复绑定");
        }

        //绑定家人
        familyMemberElder = new FamilyMemberElder();
        familyMemberElder.setFamilyMemberId(familyMemberId);
        familyMemberElder.setElderId(elder.getId());
        familyMemberElder.setElderIdCardNo(elder.getIdCardNo());
        familyMemberElder.setElderName(elder.getName());
        familyMemberElder.setRemark(familyMemberElderDto.getRemark());
        save(familyMemberElder);
    }

    @Override
    public List<FamilyMemberElder> getMemberElderList() {
        Long userId = UserThreadLocal.get();
        LambdaQueryWrapper<FamilyMemberElder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FamilyMemberElder::getFamilyMemberId, userId).select(FamilyMemberElder::getId, FamilyMemberElder::getElderId, FamilyMemberElder::getElderName, FamilyMemberElder::getFamilyMemberId);
        return familyMemberElderMapper.selectList(queryWrapper);
    }

    /**
     * 查询已绑定的家人床位设备信息
     *
     * @return
     */
    @Override
    public List<FamilyMemberElderVo> getMemberElderDetailList() {
        Long memberId = UserThreadLocal.get();
        return familyMemberElderMapper.getMemberElderDetailList(memberId);
    }

    /**
     * 查询健康数据
     *
     * @param deviceDto
     * @return
     */
    @Override
    public Map<String, DevicePropertyStatusVo> queryDevicePropertyStatus(DeviceDto deviceDto) {
        List<DeviceDataVo> deviceDataVos = familyMemberElderMapper.queryDevicePropertyStatus(deviceDto);
        deviceDataVos.forEach(deviceDataVo -> {
            switch (deviceDataVo.getIdentifier()) {
                case "HeartRate":
                    deviceDataVo.setName("心率");
                    break;
                case "BatteryPercentage":
                    deviceDataVo.setName("用电量");
                    break;
                case "BodyTemp":
                    deviceDataVo.setName("体温");
                    deviceDataVo.setDataType("double");
                    break;
                case "xueyang":
                    deviceDataVo.setName("血氧");
                    break;
            }
        });
        DevicePropertyStatusVo devicePropertyStatusVo = new DevicePropertyStatusVo();
        devicePropertyStatusVo.setPropertyStatusInfo(deviceDataVos);
        HashMap<String, DevicePropertyStatusVo> map = new HashMap<>();
        map.put("list", devicePropertyStatusVo);
        return map;
    }

    /**
     * 按天统计查询指标数据
     *
     * @return
     */
    @Override
    public List<DeviceDataListVo> queryDeviceDataListByDay(String iotId, String functionId, String startTime, String endTime) {
        String dateTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date(Long.parseLong(startTime)));
        return familyMemberElderMapper.queryDeviceDataListByDay(iotId, functionId, dateTime);
    }

    /**
     * 按周统计查询指标数据
     *
     * @return
     */
    @Override
    public List<DeviceDataListVo> queryDeviceDataListByWeek(String iotId, String functionId, String startTime, String endTime) {
        // 将毫秒值字符串转换为日期字符串
        String startDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date(Long.parseLong(startTime)));
        String endDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date(Long.parseLong(endTime)));
        return familyMemberElderMapper.queryDeviceDataListByWeek(iotId, functionId, startDate, endDate);
    }
}
